package com.jevons.geo_edit.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jevons.geo_edit.R;
import com.jevons.geo_edit.adapter.OperationHistoryAdapter;
import com.jevons.geo_edit.databinding.FragmentGisMapHistoryBinding;
import com.jevons.geo_edit.geopackage.GisMapGeoManger;
import com.jevons.geo_edit.model.GisOpHistory;
import com.jevons.muffin.eventbus.CustomEvent;
import com.jevons.muffin.fragment.BaseBindingFragment;
import com.jevons.muffin.utilcode.StringUtils;
import com.jevons.muffin.vm.BasePresenter;
import com.jevons.muffin.widget.RecycleViewDivider;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


/**
 * Copyright 2020 JxGIS
 *
 * @file GisOpHistoryFragment
 * @auth linaisheng
 * Created on 2023/6/21.
 * Description：
 */
public class GisOpHistoryFragment extends BaseBindingFragment<FragmentGisMapHistoryBinding, BasePresenter> {

    public static final String EVENT_LOAD_OP_HISTORY = "loadOpHistory";
    public static final String EVENT_LOAD_PROJECT_OPEN_STATE = "loadProjectOpenSate";

    private GisMapGeoManger gisMapGeoManger;

    private String projectId;
    private String currentHistoryId;
    private List<GisOpHistory> historyList = new ArrayList<>();
    private OperationHistoryAdapter historyAdapter;


    @Override
    public int getContentView() {
        return R.layout.fragment_gis_map_history;
    }

    @Override
    public void onInitView(Bundle savedInstanceState) {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        dataBinding.recyclerView.addItemDecoration(new RecycleViewDivider(getContext(),
                LinearLayoutManager.VERTICAL));
        historyAdapter = new OperationHistoryAdapter(getContext(), historyList);
        historyAdapter.bindToRecyclerView(dataBinding.recyclerView);
        historyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GisOpHistory history = historyAdapter.getItem(position);
                if (position == 0) {
                    EventBus.getDefault().post(new CustomEvent<>(EVENT_LOAD_PROJECT_OPEN_STATE, history));
                } else {
                    EventBus.getDefault().post(new CustomEvent<>(EVENT_LOAD_OP_HISTORY, history));
                }
            }
        });

        queryDataList();
    }

    public GisMapGeoManger getGisMapGeoManger() {
        return gisMapGeoManger;
    }

    public void setProjectData(GisMapGeoManger gisMapGeoManger, String projectId, String currentHistoryId) {
        this.projectId = projectId;
        this.currentHistoryId = currentHistoryId;
        this.gisMapGeoManger = gisMapGeoManger;
    }

    public void queryDataList(String currentHistoryId) {
        this.currentHistoryId = currentHistoryId;
        queryDataList();
    }

    private void queryDataList() {
        historyList = gisMapGeoManger.queryGisOpHistoryList(projectId);
        historyList.add(0, new GisOpHistory("", projectId, "openProject"));
        historyAdapter.setNewData(historyList);

        notifyChooseHistoryItem(currentHistoryId);
    }

    public void notifyChooseHistoryItem(String historyId) {
        currentHistoryId = historyId;
        if (StringUtils.isNotEmpty(historyId)) {
            historyAdapter.notifySelectedItem(historyId);
        } else {
            historyAdapter.notifySelectedItem("");
        }
    }

}